package com.oa.ylms.apps.dao.goods.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.dao.goods.IGoodsOrderDao;
import com.oa.ylms.apps.entity.goods.GoodCheckValue;
import com.oa.ylms.apps.entity.goods.GoodOrderDetailValue;
import com.oa.ylms.apps.entity.goods.GoodOrderValue;

public class GoodsOrderDaoImpl extends HibernateDaoSupport implements
		IGoodsOrderDao {
	private List<GoodOrderValue> orderList;
	private int totalNumber;
	public GoodOrderValue apply(GoodOrderValue goodOrder) throws Exception {
		if (goodOrder != null) {
			this.getHibernateTemplate().save(goodOrder);
			return goodOrder;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodOrderValue> queryPersonalOrder(
			final GoodOrderValue goodOrder, final int rows, final int pageNo) {

		if (goodOrder != null) {
			String hqlStr = "from GoodOrderValue gov where gov.staffInfo=? ";
			if (goodOrder.getStates() !=null) {
				if (goodOrder.getStates() != IOrderStates.CHECKING) {
					hqlStr += " AND gov.states=?";
				} else {
					hqlStr += " AND gov.states>=0";
				}
			}
			hqlStr += " ORDER by gov.createTime DESC";
			final String hql = hqlStr;
			orderList = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							query.setEntity(0, goodOrder.getStaffInfo());
							if (goodOrder.getStates() != null
									&& goodOrder.getStates() != IOrderStates.CHECKING) {
								query.setInteger(1, goodOrder.getStates());
							}
							if (pageNo != -1 && rows != -1) {
								query.setFirstResult(rows * (pageNo - 1));
								query.setMaxResults(rows);
							}
							orderList = query.list();
							return orderList;
						}
					});
			return orderList;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public int countPersonalTpages(final GoodOrderValue goodOrder,
			final int rows) throws Exception {
		List<GoodOrderValue> list = queryPersonalOrder(goodOrder, -1, -1);
		totalNumber=list.size();
		return calcPageCountFromList(list, rows);
	}

	private int calcPageCountFromList(List<GoodOrderValue> items, int rows) {
		if (items == null || items.size() == 0)
			return 0;
		int pageNumbers = 0;
		int numbers = items.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	@SuppressWarnings("unchecked")
	public List<GoodOrderValue> queryCheckOrder(final GoodOrderValue goodOrder,
			final int rows, final int pageNo) throws Exception {
		if (goodOrder != null) {
			final String hql = "from GoodOrderValue where states=?";
			orderList = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							query.setInteger(0, goodOrder.getStates());
							if (pageNo != -1 && rows != -1) {
								query.setFirstResult(rows * (pageNo - 1));
								query.setMaxResults(rows);
							}
							orderList = query.list();
							return orderList;
						}
					});
			return orderList;
		}
		return null;
	}

	public int countCheckOrderTPage(final GoodOrderValue goodOrder,
			final int rows) throws Exception {
		List<GoodOrderValue> list = queryCheckOrder(goodOrder, -1, -1);
		return calcPageCountFromList(list, rows);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodOrderValue> queryCheckOrderByState(
			final GoodCheckValue goodCheck,final int rows,final int pageNo) {
		if (goodCheck != null) {
			final String hql = "select gov from GoodOrderValue gov,GoodCheckValue gcv where gov.orderId =gcv.goodOrder.orderId  and gcv.isPassed=? and gcv.checkOrder=?";
			orderList = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							query.setInteger(0, goodCheck.getIsPassed());
							query.setInteger(1, goodCheck.getCheckOrder());
							if (pageNo != -1 && rows != -1) {
								query.setFirstResult(rows * (pageNo - 1));
								query.setMaxResults(rows);
							}
							orderList = query.list();
							return orderList;
						}
					});
			return orderList;
		}
		return null;
	}
	public int countCheckOrderByStateTPage(GoodCheckValue goodCheck,
			final int rows) throws Exception {
		List<GoodOrderValue> list = queryCheckOrderByState(goodCheck, -1, -1);
		return calcPageCountFromList(list, rows);
	}

	@SuppressWarnings("unchecked")
	public List<GoodOrderDetailValue> queryDetailByOrder(
			GoodOrderValue goodOrder) throws Exception {
		if (goodOrder != null) {
			String hql = "from GoodOrderDetailValue godv where godv.goodOrder=?";
			return this.getHibernateTemplate().find(hql, goodOrder);
		}
		return null;
	}

	public GoodOrderValue queryById(GoodOrderValue goodOrder) throws Exception {
		if (goodOrder != null) {
			return (GoodOrderValue) this.getHibernateTemplate().get(
					GoodOrderValue.class, goodOrder.getOrderId());
		}
		return null;
	}

	public boolean modifyOrderState(GoodOrderValue goodOrder) throws Exception {
		if (goodOrder != null) {
			try {
				this.getHibernateTemplate().update(goodOrder);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	public int addGoodCheck(GoodCheckValue goodCheck) throws Exception {
		if (goodCheck != null) {
			this.getHibernateTemplate().save(goodCheck);
			return goodCheck.getId();
		}
		return 0;
	}

	public List<GoodOrderValue> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<GoodOrderValue> orderList) {
		this.orderList = orderList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodOrderValue> queryReturnOrder() {
		try{
		return this.getHibernateTemplate().find("from GoodOrderValue gov where gov.states=-2 ");
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public int getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(int totalNumber) {
		this.totalNumber = totalNumber;
	}
	public int getNumber(){
		return totalNumber;
	}
}
